package com.nf.health.app.datamain.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;

import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/3/31.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description 血压测量结果展示类
 * @email he_pann@163.com
 */
public class BloodSugarMeasuredResultActivity extends BaseActivity {

    @Bind(R.id.iv_measured_result_bk)
    ImageView ivMeasuredResultBk;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_measured_time)
    TextView tvMeasuredTime;
    @Bind(R.id.tv_measured_result)
    TextView tvMeasuredResult;
    @Bind(R.id.btn_measure)
    Button btnMeasure;

    private String[] time_range = {"凌晨", "早餐前", "早餐后", "午餐前", "午餐后", "晚餐前", "晚餐后", "睡前"};
    int[] images={
            R.drawable.measure_result_xty1,
            R.drawable.measure_result_xty2,
            R.drawable.measure_result_xty3
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_blood_sugar_measured_result);
        ButterKnife.bind(this);

        initView();
        initListener();
    }

    private void initListener() {
        btnMeasure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        ViewUtil.setText(this, R.id.titlebar_title_tv, "测量结果");
        int measuredTime = PreferenceHelper.getInt(PreferenceHelper.BLOOD_SUGAR_MEASURED_TIME, 5);
        String measuredResult = PreferenceHelper.getString(PreferenceHelper.BLOOD_SUGAR_MEASURED_VALUE, "6.0");
        int bloodSugarLevel = getBloodSugarLevel(measuredTime,measuredResult);
        ivMeasuredResultBk.setBackgroundResource(images[bloodSugarLevel]);
        tvMeasuredTime.setText(time_range[measuredTime - 1]);
        tvMeasuredResult.setText(measuredResult);
        tvDate.setText(DateUtil.date2HHmm(new Date()));
    }

    /**
     * 根据测量时间和测量值判断血糖健康范围
     * @param measuredTime 测量时间 1- 8
     * @param measuredResult 测量结果
     * @return 0 低血糖 1 正常 2 糖尿病
     */
    private int getBloodSugarLevel(int measuredTime, String measuredResult) {
        float value = Float.valueOf(measuredResult);
        if (value < 2.8f){//小于2.8 不管空腹与否都是低血糖
            return 0;
        }
        if (measuredTime == 3 || measuredTime == 5 || measuredTime ==7){ //餐后血糖判断
            if (value >= 11.1f){//饭后高于11.1为糖尿病
                return 2;
            }
        }else{//饭前血糖判断
            if (value >= 7.0f){//饭前高于7.0为糖尿病
                return 2;
            }
        }
        return 1;
    }

}
